home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Development Platforms / LISP Related / Generalized LISP / Glisp 1.2 / Source files / Scanner.glisp < prev   
Encoding:
Text File  |  1990-08-15  |  9.3 KB  |  308 lines  |  [TEXT/CCL ]

  1.  
  2. ~---------------------------------------------------------------------------------------~
  3. ~                Glisp scanner definition                ~
  4. ~---------------------------------------------------------------------------------------~
  5.  
  6. -Mlisp-
  7.  
  8. export('(
  9.     \!  \@  \#  \$  \%  \^  \&  \*  \(  \)  \_  \+  \-  \=  \{  \}
  10.     \[  \]  \:  \"  \;  \'  \<  \>  \?  \,  \.  \/  \~  \`  \|  \\
  11.     \:\=    \<\=    \>\=    \/\=
  12.  
  13.     ~ option characters
  14.     \¡  \™  \£  \¢  \∞  \§  \¶  \•  \ª  \º  \–  \≠  \∑  \´  \®  \†  \¥  \¨  \^  \π
  15.     \“  \‘  \∂  \ƒ  \©  \Δ  \¬  \…  \Ω  \≈  \√  \∫  \µ  \≤  \≥  \÷  \«  \°  \—  \±
  16.     \∏  \”  \’  \  \◊  \¿  \»
  17.     \æ  \œ  \ç  \ø  \å  \ß
  18.     \Æ  \Œ  \Ç  \Ø  \Å
  19. ), `:glisp);
  20.  
  21.  
  22. global `*lisp-readtable*, `*glisp-readtable*, `*glisp-sexp-readtable* ;
  23.  
  24.  
  25. ~---------------------------------------------------------------------------------------~
  26. ~                Glisp character set                    ~
  27. ~---------------------------------------------------------------------------------------~
  28. `#|
  29. Letters
  30.     a b c d e f g h i j k l m n o p q r s t u v w x y z
  31.     A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
  32.     ! ? _ &                ~ special characters for use in names
  33.     æ œ ç ø å ß            ~ non-Greek international letters (ß is German)
  34.     Æ Œ Ç Ø Å
  35.     \<anything>
  36.  
  37. Digits
  38.     0 1 2 3 4 5 6 7 8 9        ~ same as Common Lisp
  39.  
  40. Special characters
  41.     Delimiters      @ # $ % ^   * ( )   + - = { } [ ] :   ; ' < >   , . /     |
  42.     Letters        !           &       _                           ?
  43.     Comment                                                                 ~
  44.     String                                                "
  45.     Glisp-to-Lisp                                                             `   \
  46.     Option-character letters
  47.         æ œ ç ø å ß Æ Œ Ç Ø Å
  48.     Option-character delimiters
  49.         ¡ ™ £ ¢ ∞ § ¶ • ª º – ≠ ∑ ´ ® † ¥ ¨ ^ π
  50.         “ ‘ ∂ ƒ © Δ ¬ … Ω ≈ √ ∫ µ ≤ ≥ ÷ « ° — ±
  51.         ∏ ” ’  ◊ ¿ »
  52.  
  53. String
  54.     " ... "                ~ same as Common Lisp
  55.  
  56. Comment
  57.     ~ ... <newline>            ~ Common Lisp's is:  ; ... <newline>
  58.  
  59. Interface to Lisp
  60.     ` <s-expression>        ~ ` also has its usual Common Lisp meanings
  61.  
  62. The international characters        æ œ ç ø å ß Æ Œ Ç Ø Å
  63. and the special characters        ! ? _ &
  64. are considered to be LETTERS, not DELIMITERS.
  65.  
  66. |# nil;        ~ the 'nil' is to keep the Lisp reader from getting confused
  67.  
  68.  
  69. ~---------------------------------------------------------------------------------------~
  70. ~                    Glisp scanner                    ~
  71. ~---------------------------------------------------------------------------------------~
  72.  
  73. `*lisp-readtable*  := `*readtable* ;        ~ the standard Common Lisp readtable
  74.  
  75. `*glisp-readtable* := `copy-readtable(nil);    ~ start Glisp out the same as Common Lisp
  76.  
  77.  
  78. for c in                    ~ additional letters:
  79.     '(\!  \?  \_  \&            ~    special symbols
  80.       \æ  \œ  \ç  \ø  \å  \ß        ~    international letters
  81.       \Æ  \Œ  \Ç  \Ø  \Å )
  82.     do `set-syntax-from-char(
  83.         character(c),
  84.         `#\z,                ~ logically equivalent to 'z'
  85.         `*glisp-readtable*,
  86.         `*lisp-readtable*);
  87.  
  88.  
  89. for c in                    ~ additional props for some delimiters
  90.     '(    \@  \#  \$  \%  \^      \*  \(  \)      \+  \-  \=  \{  \}
  91.       \[  \]  \:  \"  \;  \'  \<  \>      \,  \.  \/  \~  \`  \|  \\
  92.       \¡  \™  \£  \¢  \∞  \§  \¶  \•  \ª  \º  \–  \≠  \∑  \´  \®  \†  \¥  \¨  \^  \π
  93.       \“  \‘  \∂  \ƒ  \©  \Δ  \¬  \…  \Ω  \≈  \√  \∫  \µ  \≤  \≥  \÷  \«  \°  \—  \±
  94.       \∏  \”  \’  \  \◊  \¿  \» )
  95.     do begin
  96.     c.delimiter := t;
  97.     if c member '(\" \~ \` \\) then        ~ these are handled specially
  98.         return nil;
  99.     `set-syntax-from-char(
  100.         character(c),
  101.         `#\, ,                ~ logically equivalent to a comma
  102.         `*glisp-readtable*,
  103.         `*lisp-readtable*);
  104.     eval {'`set-macro-character,        ~ single character reader
  105.         character(c),
  106.         `(function (lambda (stream char) (quote ,c))),
  107.         nil,                ~ terminating macro character
  108.         `*glisp-readtable* };
  109.     end;
  110.  
  111.  
  112. for c in                    ~ other delimiters
  113.     {!eof, '\:\=,  '\<\=,  '\>\=,  '\/\= }    ~ end-of-file and 2-character delimiters
  114.     do c.delimiter := t;
  115.  
  116.  
  117. `set-dispatch-macro-character(            ~ make #$ be a reader macro in Lisp
  118.     `#\#,
  119.     `#\$,
  120.     function(lambda (stream, char, x) =
  121.         {'vEval, pVariable(read(stream, nil, !eof, t), t)}),
  122.     `*lisp-readtable*);
  123.  
  124.  
  125. `set-syntax-from-char(                ~ ~ = comment character:
  126.     `#\~ ,                    ~    ~ ... <end-of-line>
  127.     `#\; ,
  128.     `*glisp-readtable*,
  129.     `*lisp-readtable*);
  130.  
  131.  
  132. `set-macro-character(                ~ ` = interface to Lisp:
  133.     `#\` ,                    ~    `<Lisp s-expression>
  134.     function(lambda (stream, x) =
  135.         begin
  136.         `unread-char(`#\`, stream);        ~ put the ` back
  137.         x := lispRead(stream, nil, !eof, nil);    ~ and let Lisp read it
  138.         if consp(x) and car(x) eq 'quote and consp(cdr x) and null cddr(x) then
  139.             x := cadr(x);
  140.         return x;
  141.         end),
  142.     nil,                    ~ terminating macro character
  143.     `*glisp-readtable*);
  144.  
  145.  
  146. `set-macro-character(                ~ treat := as a single atom
  147.     `#\: ,
  148.     function(lambda (stream, char) =
  149.         if `char=(`peek-char(nil, stream, nil, !eofchar, t), `#\=) then
  150.             `read-char(stream, nil, !eofchar, t) also
  151.             '\:\=
  152.         else '\:),
  153.     nil,                    ~ terminating macro character
  154.     `*glisp-readtable*);
  155.  
  156.  
  157. `set-macro-character(                ~ treat <= as a single atom
  158.     `#\< ,
  159.     function(lambda (stream, char) =
  160.         if `char=(`peek-char(nil, stream, nil, !eofchar, t), `#\=) then
  161.             `read-char(stream, nil, !eofchar, t) also
  162.             '\<\=
  163.         else '\<),
  164.     nil,                    ~ terminating macro character
  165.     `*glisp-readtable*);
  166.  
  167.  
  168. `set-macro-character(                ~ treat >= as a single atom
  169.     `#\> ,
  170.     function(lambda (stream, char) =
  171.         if `char=(`peek-char(nil, stream, nil, !eofchar, t), `#\=) then
  172.             `read-char(stream, nil, !eofchar, t) also
  173.             '\>\=
  174.         else '\>),
  175.     nil,                    ~ terminating macro character
  176.     `*glisp-readtable*);
  177.  
  178.  
  179. `set-macro-character(                ~ treat /= as a single atom
  180.     `#\/ ,
  181.     function(lambda (stream, char) =
  182.         if `char=(`peek-char(nil, stream, nil, !eofchar, t), `#\=) then
  183.             `read-char(stream, nil, !eofchar, t) also
  184.             '\/\=
  185.         else '\/),
  186.     nil,                    ~ terminating macro character
  187.     `*glisp-readtable*);
  188.  
  189.  
  190. `set-macro-character(                ~ ^ -> expt
  191.     `#\^ ,
  192.     function(lambda (stream, char) = 'expt),
  193.     nil,                    ~ terminating macro character
  194.     `*glisp-readtable*);
  195.  
  196.  
  197. ~---------------------------------------------------------------------------------------~
  198. ~                Glisp s-expression reader                ~
  199. ~---------------------------------------------------------------------------------------~
  200. ~
  201. ~ Reads an s-expression consisting of Glisp tokens.  The structure of the s-expression
  202. ~ conforms to Lisp's definition.
  203.  
  204.  
  205. `*glisp-sexp-readtable* := `copy-readtable(`*glisp-readtable*);
  206.  
  207. `set-syntax-from-char(`#\(, `#\(, `*glisp-sexp-readtable*, `*lisp-readtable*);
  208.  
  209. ~`set-syntax-from-char(`#\., `#\., `*glisp-sexp-readtable*, `*lisp-readtable*);
  210.  
  211. `set-macro-character(
  212.     `#\( ,
  213.     `get-macro-character(`#\(, `*lisp-readtable*),
  214.     nil,                    ~ terminating macro character
  215.     `*glisp-sexp-readtable*);
  216.  
  217.  
  218. ~---------------------------------------------------------------------------------------~
  219. ~                    Symbols                        ~
  220. ~---------------------------------------------------------------------------------------~
  221. ~
  222. ~ associative functions:  a + b + c  ->  (+ a b c)
  223. ~ these may take any number of arguments; nested calls are linearized
  224.  
  225. for sym in '`(
  226.         + - * /
  227.         AND OR APPEND NCONC
  228.         =  /=  <  <=  >  >=
  229.         CHAR<     CHAR<=            CHAR>             CHAR>=
  230.         CHAR=     CHAR-EQUAL        CHAR-LESSP        CHAR-GREATERP
  231.         CHAR/=    CHAR-NOT-EQUAL    CHAR-NOT-LESSP    CHAR-NOT-GREATERP
  232.         STRING<   STRING<=          STRING>           STRING>=
  233.         STRING=   STRING-EQUAL      STRING-LESSP      STRING-GREATERP
  234.         STRING/=  STRING-NOT-EQUAL  STRING-NOT-LESSP  STRING-NOT-GREATERP
  235.     ) do sym.associative := t;
  236.  
  237.  
  238. ~ prefix functions:  not x  ->  (not x)
  239. ~ all take exactly one argument with no optionals
  240. ~ functions declared 'prefix' may be used without parentheses around their argument
  241.  
  242. for sym in '(
  243.     ~ a few common ones (users can declare others)
  244.         \+ \-
  245.         NOT NULL ATOM
  246.         EVAL GO
  247.  
  248.     ~ all car/cdr list selectors
  249.         CAR    CAAAAR    FIRST
  250.         CDR    CAAADR    SECOND
  251.         CAAR    CAADAR    THIRD
  252.         CADR    CAADDR    FOURTH
  253.         CDAR    CADAAR    FIFTH
  254.         CDDR    CADADR    SIXTH
  255.         CAAAR    CADDAR    SEVENTH
  256.         CAADR    CADDDR    EIGHT
  257.         CADAR    CDAAAR    NINTH
  258.         CADDR    CDAADR    TENTH
  259.         CDAAR    CDADAR    REST
  260.         CDADR    CDADDR
  261.         CDDAR    CDDAAR
  262.         CDDDR    CDDADR
  263.         CDDDAR
  264.         CDDDDR
  265.     ) do sym.prefix := t;
  266.  
  267.  
  268. ~---------------------------------------------------------------------------------------~
  269. ~                Operator precedence rules                ~
  270. ~---------------------------------------------------------------------------------------~
  271. ~
  272. ~ Assign left and right binding powers to selected infix operators.
  273. ~ Infix operators not explicitly assigned have the default binding powers.
  274. ~ If the left binding power is less than the right, the operator is left associative;
  275. ~ e.g.        a - b + c    ->  (+ (- a b) c)
  276. ~ but        a cons b cons c    ->  (cons a (cons b c))
  277.  
  278. for l in '`(
  279.     (1001  0 \:= SET SETQ SETF PSETQ)
  280.  
  281.         (800 850 EXPT)
  282.  
  283.         (700 750 * /)
  284.         
  285.         (600 650 + -)
  286.         
  287.         (500 550 DEFAULT)
  288.         
  289.         (450 400 CONS APPEND REVAPPEND NCONC NRECONC CAT CONCATENATE)
  290.        
  291.     (300 350 =   EQ  EQL  EQUAL  EQUALP
  292.                  /= NEQ NEQL NEQUAL NEQUALP
  293.                  <         <=                >                 >=
  294.                  CHAR<     CHAR<=            CHAR>             CHAR>=
  295.                  CHAR=     CHAR-EQUAL        CHAR-LESSP        CHAR-GREATERP
  296.                  CHAR/=    CHAR-NOT-EQUAL    CHAR-NOT-LESSP    CHAR-NOT-GREATERP
  297.                  STRING<   STRING<=          STRING>           STRING>=
  298.                  STRING=   STRING-EQUAL      STRING-LESSP      STRING-GREATERP
  299.                  STRING/=  STRING-NOT-EQUAL  STRING-NOT-LESSP  STRING-NOT-GREATERP)
  300.  
  301.         (200 250 AND)
  302.         
  303.         (100 150 OR)
  304.  
  305.     ) do for sym in cddr(l) do
  306.         sym.left  := first(l) also
  307.         sym.right := second(l);
  308.